package jarfinder;

import java.io.File;

import jarfinder.View.ViewContentProvider;
import jarfinder.View.ViewLabelProvider;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.swt.layout.GridData;


public class View extends ViewPart
{
   public static final String ID = "JarFinder.view";  //  @jve:decl-index=0:

   private TableViewer viewer;
   
   private Composite top = null;
   private Label label = null;
   private Text pattern = null;
   private Label label1 = null;
   private Text startDir = null;
   private Button browseButton = null;
   private Button seekButton = null;
   private Group reportType = null;
   private Button allLinesButton = null;
   private Button fileCountButton = null;

   private Text reportText = null;
   private DirectoryDialog dirDialog;

   private Button clearButton = null;
   

   /**
    * The content provider class is responsible for providing objects to the
    * view. It can wrap existing objects in adapters or simply return objects
    * as-is. These objects may be sensitive to the current input of the view,
    * or ignore it and always show the same content (like Task List, for
    * example).
    */
   class ViewContentProvider implements IStructuredContentProvider {
      public void inputChanged(Viewer v, Object oldInput, Object newInput) {
      }

      public void dispose() {
      }

      public Object[] getElements(Object parent) {
         return new String[] { "One", "Two", "Three" };
      }
   }

   class ViewLabelProvider extends LabelProvider implements
         ITableLabelProvider {
      public String getColumnText(Object obj, int index) {
         return getText(obj);
      }

      public Image getColumnImage(Object obj, int index) {
         return getImage(obj);
      }

      public Image getImage(Object obj) {
         return PlatformUI.getWorkbench().getSharedImages().getImage(
               ISharedImages.IMG_OBJ_ELEMENT);
      }
   }

   /**
    * This is a callback that will allow us to create the viewer and initialize
    * it.
    */
   public void createPartControlOld(Composite parent) {
      viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
            | SWT.V_SCROLL);
      viewer.setContentProvider(new ViewContentProvider());
      viewer.setLabelProvider(new ViewLabelProvider());
      viewer.setInput(getViewSite());
   }
   @Override
   public void createPartControl(Composite parent)
   {

      GridData gridData2 = new GridData();
      gridData2.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
      GridData gridData9 = new GridData();
      gridData9.horizontalAlignment = org.eclipse.swt.layout.GridData.END;
      GridData gridData8 = new GridData();
      gridData8.horizontalAlignment = org.eclipse.swt.layout.GridData.END;
      GridData gridData7 = new GridData();
      gridData7.widthHint = 300;
      gridData7.horizontalSpan = 2;
      GridData gridData6 = new GridData();
      gridData6.widthHint = 300;
      gridData6.horizontalSpan = 2;
      GridData gridData3 = new GridData();
      gridData3.horizontalAlignment = org.eclipse.swt.layout.GridData.CENTER;
      GridData gridData = new GridData();
      gridData.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
      gridData.horizontalSpan = 9;
      gridData.verticalSpan = 4;
      gridData.heightHint = -1;
      gridData.widthHint = 400;
      gridData.grabExcessVerticalSpace = true;
      gridData.grabExcessHorizontalSpace = true;
      gridData.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
      GridLayout gridLayout = new GridLayout();
      gridLayout.numColumns = 9;
      top = new Composite(parent, SWT.NONE);
      top.setLayout(gridLayout);
      label = new Label(top, SWT.NONE);
      label.setText("Seek Pattern");
      label.setLayoutData(gridData9);
      pattern = new Text(top, SWT.BORDER);
      pattern.setLayoutData(gridData7);
      Label filler15 = new Label(top, SWT.NONE);
      Label filler6 = new Label(top, SWT.NONE);
      Label filler7 = new Label(top, SWT.NONE);
      Label filler10 = new Label(top, SWT.NONE);
      createReportType();
      Label filler115 = new Label(top, SWT.NONE);
      label1 = new Label(top, SWT.NONE);
      label1.setText("Start Dir");
      label1.setLayoutData(gridData8);
      startDir = new Text(top, SWT.BORDER);
      startDir.setLayoutData(gridData6);
      browseButton = new Button(top, SWT.NONE);
      browseButton.setText("Browse");
      browseButton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter()
      {
         public void widgetSelected(org.eclipse.swt.events.SelectionEvent e)
         {
            dirDialog.setFilterPath(startDir.getText());
            String dir = dirDialog.open();
            if (dir != null)
            {
               startDir.setText(dir);
            }
         }
      });
      Label filler4 = new Label(top, SWT.NONE);
      Label filler5 = new Label(top, SWT.NONE);
      Label filler9 = new Label(top, SWT.NONE);
      Label filler114 = new Label(top, SWT.NONE);
      Label filler1 = new Label(top, SWT.NONE);
      Label filler14 = new Label(top, SWT.NONE);
      seekButton = new Button(top, SWT.NONE);
      seekButton.setText("Seek");
      seekButton.setLayoutData(gridData3);
      seekButton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter()
      {
         public void widgetSelected(org.eclipse.swt.events.SelectionEvent e)
         {
            JarFinder jf = new JarFinder();
            jf.addListener(new SimpleListener());
            File dir = new File(startDir.getText());
            //Cursor originalCursor = seekButton.get
            boolean allLines = allLinesButton.getSelection();
            String pat = pattern.getText();
            String result = allLines ? jf.process(dir, pat) : jf.processCount(dir, pat);
            reportText.append(result);
         }
      });
      clearButton = new Button(top, SWT.NONE);
      clearButton.setText("Clear");
      clearButton.setLayoutData(gridData2);
      clearButton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter()
      {
         public void widgetSelected(org.eclipse.swt.events.SelectionEvent e)
         {
            reportText.setText("");
         }
      });
      Label filler3 = new Label(top, SWT.NONE);
      Label filler8 = new Label(top, SWT.NONE);
      Label filler111 = new Label(top, SWT.NONE);
      Label filler11 = new Label(top, SWT.NONE);
      Label filler12 = new Label(top, SWT.NONE);
      reportText = new Text(top, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER | SWT.H_SCROLL);
      reportText.setEditable(false);
      reportText.setText("");
      reportText.setLayoutData(gridData);
      dirDialog = new DirectoryDialog(this.getViewSite().getShell());
   }


   /**
    * Passing the focus request to the viewer's control.
    */
   public void setFocus() {
      pattern.setFocus();
   }
   /**
    * This method initializes reportType  
    *
    */
   private void createReportType()
   {
   
      GridData gridData1 = new GridData();
      gridData1.verticalSpan = 2;
      GridLayout gridLayout1 = new GridLayout();
      gridLayout1.numColumns = 1;
      reportType = new Group(top, SWT.NONE);
      reportType.setText("Report detail:");
      reportType.setLayoutData(gridData1);
      reportType.setLayout(gridLayout1);
      allLinesButton = new Button(reportType, SWT.RADIO);
      fileCountButton = new Button(reportType, SWT.RADIO);
      fileCountButton.setText("File counts only");
      fileCountButton.setSelection(false);
      allLinesButton.setText("All matching lines");
      allLinesButton.setSelection(true);
      allLinesButton.setEnabled(true);
   }
}  //  @jve:decl-index=0:visual-constraint="10,10,694,252"
   
 